package com.niuniu.rushdorm.service.impl;

import com.niuniu.rushdorm.pojo.entity.Log;
import com.niuniu.rushdorm.service.LogService;
import com.niuniu.rushdorm.util.MQSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * @author Meng
 * 2022/11/24
 */
@Service
public class LogServiceImpl implements LogService {
    @Autowired
    MQSender mqSender;
    @Override
    public void logToDB(HttpServletRequest request, HttpServletResponse response) {
        Long uid = (Long)request.getAttribute("uid");
        String operation = request.getRequestURI();
        String ip = request.getRemoteAddr();
        Integer code = (Integer) request.getAttribute("code");
        String msg = (String) request.getAttribute("msg");
        String content = String.valueOf(code)+" "+msg+"==="+ (String) request.getAttribute("log_content");
        Long time = System.currentTimeMillis()/1000;
        Integer status = (Integer) request.getAttribute("log_status");
        Log log = new Log(null,uid,operation,ip,time,content,status);
        mqSender.sendMessage(log);
    }

//    private String getParameters(Map<String,String[]> parameterMap){
//        StringBuilder sb = new StringBuilder();
//        sb.append("{");
//        for(String s:parameterMap.keySet()){
//            sb.append(s+":"+ ArrayUtils.toString(parameterMap.get(s))+";");
//        }
//        sb.append("}");
//        return sb.toString();
//    }
}
